//
//  ProcessingOrder.swift
//  FeastBooking
//
//  Created by Imon on 2017/7/15.
//  Copyright © 2017年 Imon. All rights reserved.
//

import UIKit

class ProcessingOrderCollectionViewCell: UICollectionViewCell {
    
    let name: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.gray
        label.textAlignment = .left
        label.font = AppFont(size: 15)
        label.numberOfLines = 0
        return label
    }()
    
    let banquetHall: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.gray
        label.textAlignment = .left
        label.font = AppFont(size: 14)
        label.numberOfLines = 0
        return label
    }()
    
    let customer: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.lightGray
        label.textAlignment = .left
        label.font = AppFont(size: 14)
        label.numberOfLines = 0
        label.text = LanguageHelper.getString(key: "NO_RESULT")
        return label
    }()
    
    let date: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.lightGray
        label.textAlignment = .left
        label.font = AppFont(size: 13)
        label.numberOfLines = 0
        label.text = LanguageHelper.getString(key: "NO_RESULT")
        return label
    }()
    
    let amount: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.lightGray
        label.textAlignment = .left
        label.font = AppFont(size: 13)
        label.numberOfLines = 0
        return label
    }()
    
    let pointerLabel: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        imageView.image = UIImage(named: "pointer")
        return imageView
    }()
    
    let roleIcon: UIImageView = {
        let view = UIImageView()
        view.image = UIImage(named: "role")
        view.contentMode = .scaleAspectFill
        return view
    }()
    
    let roleLabel: UILabel = {
        let label = UILabel()
        label.font = AppFont(size: 14)
        label.textColor = proceededColor
        label.text = ""
        return label
    }()
    
    let processBar: ProcessBar = {
        return ProcessBar()
    }()
    
    let saperator: UIView = {
        let view = UIView()
        view.backgroundColor = saperatorColor
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.addSubview(name)
        self.addSubview(banquetHall)
        self.addSubview(processBar)
        self.addSubview(pointerLabel)
        self.addSubview(customer)
        self.addSubview(saperator)
    }
    
    func initValue(order: UserOrder) {
        self.name.text = order.hotel
        self.banquetHall.text = order.banquetHall
        if let tables = order.tables {
            self.amount.text = "\(tables)\(LanguageHelper.getString(key: "TABLE"))"
        } else {
            self.amount.text = "0\(LanguageHelper.getString(key: "TABLE"))"
        }
        if let date = order.date {
            self.date.text = date
        } else {
            self.date.text = LanguageHelper.getString(key: "NO_RESULT")
        }
        self.customer.text = order.contact
        self.processBar.step = orderSteps[order.orderStatus!]!
        if let roles = order.roleInOrder {
            if roles.contains("CUSTOMER") {
                //不是订单客户，隐藏日期和摆酒桌数
                self.addSubview(date)
                self.addSubview(amount)
                self.roleLabel.removeFromSuperview()
                self.roleIcon.removeFromSuperview()
            } else {
                self.date.removeFromSuperview()
                self.amount.removeFromSuperview()
                self.addSubview(roleLabel)
                self.addSubview(roleIcon)
                if roles.contains("RECOMMENDER") {
                    self.roleLabel.text = LanguageHelper.getString(key: "RECOMMENDER");
                    if roles.contains("MANAGER") {
                        self.roleLabel.text = LanguageHelper.getString(key: "RECO_MANAGER")
                    }
                } else if roles.contains("MANAGER") {
                    self.roleLabel.text = LanguageHelper.getString(key: "MANAGER")
                }
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let maxSize = CGSize(width: bounds.width, height: 40)
        let nameSize = TextSize.textSize(text: name.text!, font: name.font, maxSize: maxSize)
        name.frame = CGRect(x: 10, y: 10, width: nameSize.width, height: nameSize.height)
        let dateSize = TextSize.textSize(text: date.text!, font: date.font, maxSize: maxSize)
        date.frame = CGRect(x: bounds.width - dateSize.width - 40, y: 10, width: dateSize.width, height: dateSize.height)
        let roleSize = TextSize.textSize(text: roleLabel.text!, font: roleLabel.font, maxSize: maxSize)
        roleIcon.frame = CGRect(x: bounds.width - roleSize.width - 61, y: 14, width: 18, height: 17)
        roleLabel.frame = CGRect(x: bounds.width - roleSize.width - 40, y: 14, width: roleSize.width, height: roleSize.height)
        let banquetHallSize = TextSize.textSize(text: banquetHall.text!, font: banquetHall.font, maxSize: maxSize)
        banquetHall.frame = CGRect(x: 10, y: 10 + name.frame.height + 5, width: banquetHallSize.width, height: banquetHallSize.height)
        let customerSize = TextSize.textSize(text: customer.text!, font: customer.font, maxSize: maxSize)
        customer.frame = CGRect(x: 10, y: banquetHall.frame.origin.y + banquetHall.frame.height + 5, width: customerSize.width, height: customerSize.height)
        let amountSize = TextSize.textSize(text: amount.text!, font: amount.font, maxSize: maxSize)
        amount.frame = CGRect(x: bounds.width - amountSize.width - 40, y: 10 + name.frame.height + 5, width: amountSize.width, height: amountSize.height)
        let pointerWidth:CGFloat = 13
        
        let barWidth: CGFloat = bounds.width - 50
        processBar.frame = CGRect(x: 20, y: bounds.height - 35, width: barWidth, height: barWidth / 20 + 20)
        self.processBar.layoutSubviews()
        pointerLabel.frame = CGRect(x: bounds.width - 10 - pointerWidth, y: (bounds.height - pointerWidth) / 2, width: pointerWidth, height: pointerWidth)
        saperator.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(bounds.height - 0.5, 0, 0, 0))
    }
    
}

class HistoryOrderCollectionViewCell: UICollectionViewCell {
    
    let name: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.gray
        label.textAlignment = .left
        label.font = AppFont(size: 15)
        label.numberOfLines = 0
        return label
    }()
    
    let banquetHall: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.gray
        label.textAlignment = .left
        label.font = AppFont(size: 14)
        label.numberOfLines = 0
        return label
    }()
    
    let customer: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.lightGray
        label.textAlignment = .left
        label.font = AppFont(size: 14)
        label.numberOfLines = 0
        label.text = LanguageHelper.getString(key: "NO_RESULT")
        return label
    }()
    
    let date: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.lightGray
        label.textAlignment = .left
        label.font = AppFont(size: 13)
        label.numberOfLines = 0
        label.text = LanguageHelper.getString(key: "NO_RESULT")
        return label
    }()
    
    let amount: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.lightGray
        label.textAlignment = .left
        label.font = AppFont(size: 13)
        label.numberOfLines = 0
        return label
    }()
    
    let pointerLabel: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        imageView.image = UIImage(named: "pointer")
        return imageView
    }()
    
    let roleIcon: UIImageView = {
        let view = UIImageView()
        view.image = UIImage(named: "role")
        view.contentMode = .scaleAspectFill
        return view
    }()
    
    let roleLabel: UILabel = {
        let label = UILabel()
        label.font = AppFont(size: 14)
        label.textColor = proceededColor
        label.text = ""
        return label
    }()
    
    let finishIcon: UIImageView = {
        let view = UIImageView()
        view.image = UIImage(named: "finish")
        view.contentMode = .scaleAspectFill
        return view
    }()
    
    let statusLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .left
        label.font = AppFont(size: 15)
        label.numberOfLines = 0
        label.textColor = finishedColor
        label.text = LanguageHelper.getString(key: "DONE")
        return label
    }()
    
    
    let saperator: UIView = {
        let view = UIView()
        view.backgroundColor = saperatorColor
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.addSubview(name)
        self.addSubview(banquetHall)
        self.addSubview(pointerLabel)
        self.addSubview(customer)
        self.addSubview(finishIcon)
        self.addSubview(statusLabel)
        self.addSubview(saperator)
    }
    
    func initValue(order: UserOrder) {
        self.name.text = order.hotel
        self.banquetHall.text = order.banquetHall
        if let tables = order.tables {
            self.amount.text = "\(tables)\(LanguageHelper.getString(key: "TABLE"))"
        } else {
            self.amount.text = "0\(LanguageHelper.getString(key: "TABLE"))"
        }
        if let date = order.date {
            self.date.text = date
        } else {
            self.date.text = LanguageHelper.getString(key: "NO_RESULT")
        }
//        if let status = order.orderStatus {
//            let step = orderSteps[status]!
//            if step == 7 {
//                statusLabel.layer.borderColor = redPointColor.cgColor
//                statusLabel.textColor = redPointColor
//            } else {
//                statusLabel.layer.borderColor = finishedColor.cgColor
//                statusLabel.textColor = finishedColor
//            }
//            statusLabel.text = stepNames[step - 1]
//        }
        self.customer.text = order.contact
        if let roles = order.roleInOrder {
            if roles.contains("CUSTOMER") {
                //不是订单客户，隐藏日期和摆酒桌数
                self.addSubview(date)
                self.addSubview(amount)
                self.roleLabel.removeFromSuperview()
                self.roleIcon.removeFromSuperview()
            } else {
                self.date.removeFromSuperview()
                self.amount.removeFromSuperview()
                self.addSubview(roleLabel)
                self.addSubview(roleIcon)
                if roles.contains("RECOMMENDER") {
                    self.roleLabel.text = LanguageHelper.getString(key: "RECOMMENDER");
                    if roles.contains("MANAGER") {
                        self.roleLabel.text = LanguageHelper.getString(key: "RECO_MANAGER")
                    }
                } else if roles.contains("MANAGER") {
                    self.roleLabel.text = LanguageHelper.getString(key: "MANAGER")
                }
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let maxSize = CGSize(width: bounds.width, height: 40)
        let nameSize = TextSize.textSize(text: name.text!, font: name.font, maxSize: maxSize)
        name.frame = CGRect(x: 10, y: 10, width: nameSize.width, height: nameSize.height)
        let dateSize = TextSize.textSize(text: date.text!, font: date.font, maxSize: maxSize)
        date.frame = CGRect(x: bounds.width - dateSize.width - 40, y: 10, width: dateSize.width, height: dateSize.height)
        let roleSize = TextSize.textSize(text: roleLabel.text!, font: roleLabel.font, maxSize: maxSize)
        roleIcon.frame = CGRect(x: bounds.width - roleSize.width - 61, y: 14, width: 18, height: 17)
        roleLabel.frame = CGRect(x: bounds.width - roleSize.width - 40, y: 14, width: roleSize.width, height: roleSize.height)
        let banquetHallSize = TextSize.textSize(text: banquetHall.text!, font: banquetHall.font, maxSize: maxSize)
        banquetHall.frame = CGRect(x: 10, y: 10 + name.frame.height + 5, width: banquetHallSize.width, height: banquetHallSize.height)
        let customerSize = TextSize.textSize(text: customer.text!, font: customer.font, maxSize: maxSize)
        customer.frame = CGRect(x: 10, y: banquetHall.frame.origin.y + banquetHall.frame.height + 5, width: customerSize.width, height: customerSize.height)
        let amountSize = TextSize.textSize(text: amount.text!, font: amount.font, maxSize: maxSize)
        amount.frame = CGRect(x: bounds.width - amountSize.width - 40, y: 10 + name.frame.height + 5, width: amountSize.width, height: amountSize.height)
        let pointerWidth:CGFloat = 13
        let statusSize = TextSize.textSize(text: statusLabel.text!, font: statusLabel.font, maxSize: maxSize)
        finishIcon.frame = CGRect(x: 10, y: customer.frame.origin.y + customer.frame.height + 5, width: 18, height: 18)
        statusLabel.frame = CGRect(x: 30, y: customer.frame.origin.y + customer.frame.height + 5, width: statusSize.width, height: statusSize.height)
        
        pointerLabel.frame = CGRect(x: bounds.width - 10 - pointerWidth, y: (bounds.height - pointerWidth) / 2, width: pointerWidth, height: pointerWidth)
        saperator.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(bounds.height - 0.5, 0, 0, 0))
    }
    
}
